package com.wanban.adutils.advertisers.ironsource

import android.app.Activity
import android.view.View
import android.view.ViewGroup
import android.widget.RelativeLayout
import com.wanban.adutils.advertisers.BaseAdLoader
import com.wanban.adutils.callback.OnLoadAdCallback
import com.wanban.adutils.tools.AdConstants
import com.unity3d.mediation.LevelPlayAdError
import com.unity3d.mediation.LevelPlayAdInfo
import com.unity3d.mediation.LevelPlayAdSize
import com.unity3d.mediation.banner.LevelPlayBannerAdView
import com.unity3d.mediation.banner.LevelPlayBannerAdViewListener
import com.wanban.adutils.db.bean.AdInfo


class ISBannerAdUtils: BaseAdLoader() {

    private var levelPlayBanner: LevelPlayBannerAdView? = null

    override fun loadAdNew(activity: Activity, adInfo: AdInfo, callback: OnLoadAdCallback) {
        super.loadAdNew(activity, adInfo, callback)
        val bannerAdConfig = LevelPlayBannerAdView.Config.Builder()
        bannerAdConfig.setAdSize(LevelPlayAdSize.BANNER)
        levelPlayBanner = LevelPlayBannerAdView(activity, realUnitId, bannerAdConfig.build())
        levelPlayBanner?.setBannerListener(object: LevelPlayBannerAdViewListener {
            override fun onAdLoaded(adInfo: LevelPlayAdInfo) {
                // Ad was loaded successfully
                activity.runOnUiThread {
                    if (isTimeout) {
                        destroy()
                        return@runOnUiThread
                    }
                    setAdLoadSuccess()
                    sdkLoadFinish()
                    callback.onAdLoaded(levelPlayBanner)
                }
            }
            override fun onAdLoadFailed(error: LevelPlayAdError) {
                // Ad load failed
                activity.runOnUiThread {
                    if (isTimeout) {
                        destroy()
                        return@runOnUiThread
                    }
                    sdkLoadFinish()
                    val code = error.getErrorCode()
                    analysisErrorCode(adInfo, code)
                    callback.onAdLoadFailed(code)
                    destroy()
                }
            }
            override fun onAdDisplayed(adInfo: LevelPlayAdInfo) {
                // Ad was displayed and visible on screen
                setAdLoadShowing(true)
            }
            override fun onAdDisplayFailed(adInfo: LevelPlayAdInfo, error: LevelPlayAdError) {
                // Optional. Ad failed to be displayed on screen
                setAdLoadShowing(false)
                activity.runOnUiThread {
                    if (isTimeout) {
                        destroy()
                        return@runOnUiThread
                    }
                    sdkLoadFinish()
                    callback.onAdLoadFailed(error.getErrorCode())
                    destroy()
                }
            }
            override fun onAdClicked(adInfo: LevelPlayAdInfo) {
                // Ad was clicked
                activity.runOnUiThread {
                    callback.onAdClicked()
                }
            }
            override fun onAdExpanded(adInfo: LevelPlayAdInfo) {
                // Optional. Ad is opened on full screen
            }
            override fun onAdCollapsed(adInfo: LevelPlayAdInfo) {
                // Optional. Ad is restored to its original size
            }
            override fun onAdLeftApplication(adInfo: LevelPlayAdInfo) {
                // Optional. User pressed on the ad and was navigated out of the app
            }
        })

        levelPlayBanner?.loadAd()

    }


    override fun showAd(adRoot: ViewGroup, type: Int) {
        if (levelPlayBanner == null) {
            mOnLoadAdCallback?.onAdLoadFailed(AdConstants.ERROR_CODE_INVALID_REQUEST)
            destroy()
            return
        }
        adRoot.removeAllViews()
        adRoot.visibility = View.VISIBLE
        val params = RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT)
        adRoot.addView(levelPlayBanner, params)
    }


    override fun destroy() {
        super.destroy()
        if (levelPlayBanner == null) return
        val adRoot = levelPlayBanner?.parent as ViewGroup?
        if (adRoot != null) {
            adRoot.removeAllViews()
            adRoot.visibility = View.GONE
        }
        levelPlayBanner?.destroy()
    }
}